/*
*/
function Area() {
	var areaObj = {
		id: "area",//容器ID		
		code: "001011001001",//默认区域，缺省为此值
		callBack: function(){} //回调
	};
	this.init = function(option) {
		if(option.id)
		{
			areaObj.id = option.id;
		}
		if(option.code)
		{
			areaObj.code = option.code;
		}
		if(option.callBack)
		{
			areaObj.callBack = option.callBack;
		}
		areaObj.container = document.getElementById(areaObj.id);
		areaObj.province = document.createElement("select");
		areaObj.city = document.createElement("select");
		areaObj.county = document.createElement("select");
		areaObj.container.appendChild(areaObj.province);
		areaObj.container.appendChild(areaObj.city);
		areaObj.container.appendChild(areaObj.county);
		areaObj.province.addEventListener("change", function() {
			areaObj.change("province")
		});
		areaObj.city.addEventListener("change", function() {
			areaObj.change("city")
		});
		areaObj.county.addEventListener("change", function() {
			areaObj.change("county")
		});
		areaObj.setCode(areaObj.code)
	};
	//设置指定区域
	areaObj.setCode = function(code) {
		var pcode = code.substring(0, code.length - 6);
		var cityCode = code.substring(0, code.length - 3);
		var countyCode = code;
		areaObj.setItems("001", areaObj.province);
		areaObj.setItems(pcode, areaObj.city);
		areaObj.setItems(cityCode, areaObj.county);
		$(areaObj.province).val(pcode);
		$(areaObj.city).val(cityCode);
		$(areaObj.county).val(countyCode)
	}
	//数据装配
	areaObj.setItems = function(pcode, obj) {
		var items = jsonsql.query("select * from json where (pcode=='" + pcode + "') order by code asc", areaItems);
		var options = "";
		$.each(items, function(index, item) {
			options += "<option value='" + item.code + "'>" + item.name + "</option>"
		});
		$(obj).html(options)
	};
	//change事件
	areaObj.change = function(type) {
		if (type == "province") {
			areaObj.setItems($(areaObj.province).val(), areaObj.city);
			areaObj.setItems($(areaObj.city).val(), areaObj.county)
		}
		if (type == "city") {
			areaObj.setItems($(areaObj.city).val(), areaObj.county)
		}
		areaObj.callBack($(areaObj.county).val(),$(areaObj.county).text(),areaObj.county)
	};
	this.setCode = areaObj.setCode
}